import { type GroupNewsParams } from './apis';
import { type FilterMenuProps, ComponentType, type IResult } from 'vc/src/web';

interface ITab {
  code: string;
  name: string;
  level: number;
  values: any[];
}

export function useScreen() {
  const tabs = ref<ITab[]>([]);
  const currentTab = ref<ITab>({
    code: '',
    name: '全部',
    level: 1,
    values: []
  });

  const queryData = reactive<GroupNewsParams>({
    limit: 15,
    offset: 0,
    codeLevel1: '',
    codeLevel2: '',
    codeLevel3: '',
    years: '1',
    sortRule: ''
  });

  const initQueryData = () => {
    queryData.limit = 15;
    queryData.offset = 0;
    queryData.codeLevel1 = '';
    queryData.codeLevel2 = '';
    queryData.codeLevel3 = '';
    queryData.years = '1';
  };

  const menus = ref<Array<FilterMenuProps>>([
    {
      hide: false,
      title: '日期区间',
      component: ComponentType.SINGLE,
      options: [
        { code: '', name: '不限' },
        { code: '1', name: '近一年' },
        { code: '3', name: '近三年' },
        { code: '5', name: '近五年' }
      ],
      value: '1',
      submitFn: (val: string) => {
        queryData.years = val;
      }
    },
    {
      hide: true,
      title: '筛选',
      component: ComponentType.CASCADER,
      checkedOnly: true,
      options: [],
      submitFn: (val: IResult) => {
        let { lv1, lv2 } = val;
        if (lv1.length) {
          if (lv1.length === currentTab.value.values.length) {
            queryData.codeLevel1 = currentTab.value.code;
            queryData.codeLevel2 = '';
            queryData.codeLevel3 = '';
          } else {
            lv1.forEach((lv1code) => {
              const len = lv1code.length;
              lv2 = lv2.filter((lv2code) => lv2code.slice(0, len) !== lv1code);
            });
            queryData.codeLevel1 = '';
            queryData.codeLevel2 = lv1.join();
            queryData.codeLevel3 = lv2.join();
          }
        } else {
          queryData.codeLevel1 = '';
          queryData.codeLevel2 = '';
          queryData.codeLevel3 = lv2.join();
        }
      }
    }
  ]);

  return {
    tabs,
    currentTab,
    queryData,
    initQueryData,
    menus
  };
}
